/* ------------------------------------------------
 *
 * Extended Helper for jQuery
 * (c) Roy (roy@kiranatama.com | roy@maysora.com)
 *
 * Dual licensed under the MIT and GPL licenses:
 * - http://www.opensource.org/licenses/mit-license.php
 * - http://www.gnu.org/licenses/gpl.html
 *
 * ------------------------------------------------
 * Requirements:
 * ------------------------------------------------
 *
 * - jQuery js framework
 * - jquery.extended_helper.js
 *
 * ------------------------------------------------
 * Usage:
 * ------------------------------------------------
 *
 * include this file after jquery.extended_helper
 *
 * ------------------------------------------------
 */


/* deprecated, use $(selector).serialize() instead
 */
function serialize(container){
  return $(':input', container).serialize();
}

/* deprecated, use $(selector).isDirty() instead
 */
function isFieldDirty(obj){
  return $(obj).isDirty();
}

/* deprecated, use $(selector).setDefault() instead
 */
function setFieldDefault(obj){
  return $(obj).setDefault();
}

/* deprecated, use $(selector).focusInput() instead
 */
function focusInput(selector, blankFirst, select){
  if(typeof selector === "undefined" || selector == null)
    selector = document;
  return $(selector).focusInput(blankFirst, select);
}

/* deprecated, use $(selector).resetInput() instead
 */
function resetFields(fields){
  return $(fields).resetInput();
}

/* deprecated, use ''.toNumber() and .toFixed(dp)
 *
 * convert string to number with dp decimal points, return 0 if str not a number
 * example: strToNum('123this will return empty string321')
 *          strToNum('1.23456', 2)
 */
function strToNum(str, dp){
  var num = str.toNumber();
  if(typeof dp != 'undefined' && dp)
    num = num.toFixed(dp);
  return num
}

/* deprecated, use $(selector).numericOnly(options)
 */
function bindNumericOnly(e){
  $(e).each(function(){
    $(this).numericOnly({
      onlyPositive: $(this).hasClass('numeric_positive'),
      onlyInteger: $(this).hasClass('numeric_integer'),
      minValue: $(this).attr('minval'),
      maxValue: $(this).attr('maxval'),
      allowBlank: !$(this).hasClass('numeric_present')
    });
  });
}

/* deprecated, use $(selector).setButton(enabled, disabledClass)
 */
function setButton(button, enabled, disabledClass){
  return $(button).setButton(enabled, disabledClass);
}

/* deprecated, use $(selector).setLink(enabled, disabledClass)
 */
function setLink(link, enabled, disabledClass){
  return $(link).setLink(enabled, disabledClass);
}

/* deprecated, use ''.sanitize()
 */
function sanitize(text){
  return text.sanitize();
}

/* deprecated, use [].compact(remove_level) instead.
 *
 * return a copy of array with all null and optionally blank or false elements removed
 * example:
 * - arrayCompact(['a',null, 0, '', false])       //=> [ 'a', 0, '', false ]  (only null removed)
 * - arrayCompact(['a',null, 0, '', false], 1)    //=> [ 'a', 0, false ]      (only null and empty string removed)
 * - arrayCompact(['a',null, 0, '', false], 2)    //=> [ 'a' ]                (all that equal to false removed)
 */
function arrayCompact(array, remove_level){
  return array.compact(remove_level);
}

/* Force expire cached tab with specified index
 * require: jQuery UI Tabs
 * example:
 * - uiTabsExpireTab($('#tabs'), 0)  // will expire tab with index 0 (first tab)
 */
function uiTabsExpireTab(tabs, index){
  $.data($('li a', tabs).get(index), 'cache.tabs', false);
  $($.data($('li a', tabs).get(index)).handle.elem.hash).html('');
}